package com.ibangnuo.framework.newLandpay.impl.request;

import com.alibaba.fastjson.JSONObject;
import com.ibangnuo.framework.newLandpay.api.NewLandpayRequest;
import com.ibangnuo.framework.newLandpay.impl.common.ResponseClient;
import com.ibangnuo.framework.newLandpay.impl.common.api.ResponseClientApi;
import com.ibangnuo.framework.newLandpay.impl.response.AliServicePayResponse;
import com.ibangnuo.framework.newLandpay.util.HttpParamsUtils;
import com.ibangnuo.framework.newLandpay.util.MD5;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.Date;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020-01-09 15:15
 */
@Data
@NoArgsConstructor
public class AliServicePayRequest  implements NewLandpayRequest {
    private static Logger LOGGER = LoggerFactory.getLogger(AliServicePayRequest.class);
    private Map<String,String> bizContent;//不进行加密的参数
    private Map<String,String> pre;//进行加密的参数
    private final String URL = "http://sandbox.starpos.com.cn/adpweb/ehpspos3/aliServicePay.json";
    /**
     * 具体接口调用
     * @return
     */
    public AliServicePayResponse execute(String key) {
        String preStr = HttpParamsUtils.buildPayValues(pre, false, true);
        String sign = MD5.sign(preStr, key, "UTF-8");
        pre.put("signValue",sign);
        ResponseClientApi responseClientApi = new ResponseClient(URL);
        String preJson = JSONObject.toJSONString(pre);
        JSONObject jsonObject = JSONObject.parseObject(preJson);
        String response = responseClientApi.request(jsonObject);
        LOGGER.info("新大陆接口响应结果{}",response);
        Map<String,String> map = JSONObject.parseObject(response, Map.class);
        if(map != null){
            AliServicePayResponse aliServicePayResponse = new AliServicePayResponse();
            aliServicePayResponse.setReturnCode(map.get("returnCode"));
            aliServicePayResponse.setSysTime(new Date(Long.parseLong(map.get("sysTime"))));
            aliServicePayResponse.setMessage(map.get("message"));
            aliServicePayResponse.setTradeNo(map.get("tradeNo"));
            aliServicePayResponse.setMercId(map.get("mercId"));
            aliServicePayResponse.setLogNo(map.get("LogNo"));
            aliServicePayResponse.setResult(map.get("result"));
            aliServicePayResponse.setOrderNo(map.get("orderNo"));
            aliServicePayResponse.setAmount(map.get("amount"));
            aliServicePayResponse.setTotalAmount(Double.parseDouble(map.get("total_amount")));
            aliServicePayResponse.setPrepayId(map.get("PrepayId"));
            aliServicePayResponse.setApiAppid(map.get("apiAppid"));
            if(StringUtils.isNotEmpty(map.get("subject"))){
                aliServicePayResponse.setSubject(map.get("subject"));
            }
            if(StringUtils.isNotEmpty(map.get("selOrderNo"))){
                aliServicePayResponse.setSelOrderNo(map.get("selOrderNo"));
            }
            if(StringUtils.isNotEmpty(map.get("attach"))){
                aliServicePayResponse.setAttach(map.get("attach"));
            }
            if(StringUtils.isNotEmpty(map.get("signValue"))){
                aliServicePayResponse.setSignValue(map.get("signValue"));
            }
            return aliServicePayResponse;

        }
        return null;
    }



}
